//package com.snow.vpnclient.util;
//
//import android.os.Bundle;
//
//import androidx.fragment.app.Fragment;
//import androidx.fragment.app.FragmentActivity;
//import androidx.fragment.app.FragmentManager;
//import androidx.fragment.app.FragmentTransaction;
//
//import com.blankj.utilcode.util.StringUtils;
//import com.snow.vpnclient.R;
//import com.snow.vpnclient.constant.SpeConsts;
//import com.snow.vpnclient.fragment.splash.SplashFragment;
//import com.snow.vpnclient.fragment.token.TokenBindingFragment;
//import com.snow.vpnclient.fragment.token.TokenUnbindFragment;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import lombok.Getter;
//
///**
// * @author liufq
// * @since 2023-04-02 14:59:09
// */
//@Getter
//public enum FragmentHelper {
//
//    INSTANCE;
//
//    private Map<String, Fragment> fragmentMap;
//
//    private Fragment currFragment;
//
//    public void init() {
//        fragmentMap = new HashMap<>();
//        currFragment = new SplashFragment();
//        fragmentMap.put(SplashFragment.class.getSimpleName(), currFragment);
//    }
//
//    public void switchFragment(FragmentActivity activity, Class<? extends Fragment> clazz) {
//        switchFragment(activity, clazz, null, true);
//    }
//
//    public void switchFragment(FragmentActivity activity, Class<? extends Fragment> clazz, boolean isReplace) {
//        switchFragment(activity, clazz, null, isReplace);
//    }
//
//    public void switchFragment(FragmentActivity activity, Class<? extends Fragment> clazz, Bundle bundle) {
//        switchFragment(activity, clazz, bundle, true);
//    }
//
//    public void switchFragment(FragmentActivity activity, Class<? extends Fragment> clazz, Bundle bundle, boolean isReplace) {
//        try {
//            Fragment nextFragment = fragmentMap.get(clazz.getSimpleName());
////            Fragment currFragment = fragmentMap.get(currentFragment);
//            if (nextFragment == null) {
//                nextFragment = clazz.newInstance();
//                if (bundle != null) {
//                    nextFragment.setArguments(bundle);
//                }
//                fragmentMap.put(clazz.getSimpleName(), nextFragment);
//            }
//
//            FragmentManager fragmentManager = activity.getSupportFragmentManager();
//            FragmentTransaction transaction = fragmentManager.beginTransaction();
//            Fragment PreviousFragment = activity.getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);//获取当前fragment
//            if (PreviousFragment != null) {
//                transaction.hide(PreviousFragment);
//            }
//            if (!nextFragment.isAdded()) {//判断是否以添加过
//                transaction.add(R.id.nav_host_fragment_content_main, nextFragment);
//                transaction.addToBackStack(null);//添加到回退栈  重要!!!
//            }
//            transaction.show(nextFragment).commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
////        try {
////            Fragment fragment = fragmentMap.get(clazz.getSimpleName());
//////            Fragment currFragment = fragmentMap.get(currentFragment);
////            if (fragment == null) {
////                fragment = clazz.newInstance();
////                if (bundle != null) {
////                    fragment.setArguments(bundle);
////                }
////                fragmentMap.put(clazz.getSimpleName(), fragment);
////            }
////
////            if (currFragment!=fragment){
////                FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
////                if (!fragment.isAdded()) { // 先判断是否被add过
////                    transaction.hide(currFragment).replace(R.id.nav_host_fragment_content_main, fragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
////                } else {
////                    transaction.hide(currFragment).show(fragment).commit(); // 隐藏当前的fragment，显示下一个
////                }
////                currFragment = fragment;
////
//////                Logger.d("fragment.isAdded()", fragment.isAdded(), "fragment=",
//////                        clazz.getSimpleName(), "currentFragment=", currentFragment);
////
////
//////                if (!clazz.getSimpleName().equals(currentFragment)) {
//////                    transaction.hide(currFragment);
//////                }
//////
//////                if (fragmentMap.get(fragment.getTag()) != null || fragment.isAdded()) {
//////                    transaction.show(fragment).addToBackStack(null).commit();
//////                } else {
//////                    if (isReplace) {
//////                        transaction.replace(R.id.nav_host_fragment_content_main, fragment, clazz.getSimpleName());
//////                    } else {
//////                        transaction.add(R.id.nav_host_fragment_content_main, fragment, clazz.getSimpleName());
//////                    }
//////                    transaction.addToBackStack(null).commit();
//////                }
//////                currentFragment = clazz.getSimpleName();
////            }
////
////        } catch (Exception e) {
////            Logger.e("fragment切换异常", e);
////            ToastUtils.showShort("fragment切换异常，" + e.getMessage());
////        }
//    }
//
//    public void back(FragmentActivity activity) {
//        Logger.d("FragmentHelper back");
////        activity.getSupportFragmentManager().popBackStackImmediate();
//        activity.getSupportFragmentManager().popBackStack();
////        activity.getFragmentManager().popBackStack();
//    }
//
//    public void switchToken(FragmentActivity activity) {
//        String secret = SpHelper.getString(SpeConsts.KEY_TOKEN_SECRET);
//        Logger.d("本地秘钥", secret);
//        if (!StringUtils.isEmpty(secret)) {
//            FragmentHelper.INSTANCE.switchFragment(activity, TokenBindingFragment.class);
//        } else {
//            FragmentHelper.INSTANCE.switchFragment(activity, TokenUnbindFragment.class);
//        }
//    }
//}
